package com.shanghaionstar.dealersorder.presenter;

import android.content.Context;

import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.dealersorder.bean.DealerOrderList;
import com.shanghaionstar.dealersorder.model.DealersOrderModel;
import com.shanghaionstar.dealersorder.view.DealersOrderView;

import rx.functions.Action1;

/**
 * Created by Yonglang Hong on 2016/8/11.
 */
public class DealersOrderPresenter extends BasePresenter {

    DealersOrderView   orderView;

    DealersOrderModel  orderModel;

    public void setOrderView(DealersOrderView orderView) {
        this.orderView = orderView;
    }

    public DealersOrderPresenter(IMVPScheduler mvpScheduler, Context context) {
        super(mvpScheduler);
        orderModel = new DealersOrderModel(context);
    }

    public void loadDealers(String latitude, String longtitude) {
        subscription = orderModel.loadDealers(latitude,longtitude)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<DealerOrderList>() {
                    @Override
                    public void call(DealerOrderList orderList) {
                        if (orderList == null || orderList.getDealers() == null || orderList.getDealers().isEmpty())
                            orderView.showEmpty();
                        else
                            orderView.showDealersList(orderList);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        orderView.showErr(throwable);
                    }
                });
    }
}
